package com.mz.mattress.client.plan;

import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

public class TaskNumCalculate {

    /**
     * 计算当前时间段的任务数量
     */
    public static int calculateTaskCount(LocalTime time, Integer totalTask) {

        TimeSegment segment = TimeSegment.getSegment(time);
        if (segment == TimeSegment.OTHER_TIME) {
            return 0; // 非活跃时间段不下发任务
        }

        // 计算当前时间段内的15分钟间隔数量
        long segmentIntervals = calculateSegmentIntervals(segment);

        // 计算每个间隔的基础任务量
        double baseTasksPerInterval = (totalTask * segment.getProportion()) / segmentIntervals;

        // 添加随机波动 (±15%)
        double randomFactor = 0.85 + 0.3 * ThreadLocalRandom.current().nextDouble();
        int taskCount = (int) (baseTasksPerInterval * randomFactor);

        return Math.max(0, taskCount);
    }

    /**
     * 计算时间段内的10分钟间隔数量
     */
    private static long calculateSegmentIntervals(TimeSegment segment) {
        if (segment.getStart() == null || segment.getEnd() == null) {
            return 0;
        }

        long startMinutes = segment.getStart().getHour() * 60L + segment.getStart().getMinute();
        long endMinutes = segment.getEnd().getHour() * 60L + segment.getEnd().getMinute();
        long durationMinutes = endMinutes - startMinutes;

        return durationMinutes / PlanTimer.TIME_SPAN; // 每15分钟一个间隔
    }


    public static void main(String[] args) throws InterruptedException {

        previewDailyDistribution(93);

        System.out.println("dd");

    }

    /**
     * 预览一天的任务分布
     */
    public static void previewDailyDistribution(int dailyTotalTasks) {
        System.out.println("🔮 === 24小时任务分布预览 ===");

        int totalPreviewTasks = 0;
        Map<TimeSegment, Integer> segmentPreview = new HashMap<>();

        for (int hour = 0; hour < 24; hour++) {
            for (int minute = 0; minute < 60; minute += PlanTimer.TIME_SPAN) {
                LocalTime time = LocalTime.of(hour, minute);
                TimeSegment segment = TimeSegment.getSegment(time);
                int taskCount = calculateTaskCount(time, dailyTotalTasks);
                totalPreviewTasks += taskCount;
                segmentPreview.merge(segment, taskCount, Integer::sum);

                if (taskCount > 0) {
//                    String progressBar = getProgressBar(taskCount, 20);
                    String progressBar = "";
                    System.out.printf("   %02d:%02d [%s] %2d 任务 %s%n",
                            hour, minute, segment.getDescription(),
                            taskCount, progressBar);
                }
            }
        }

        System.out.println("\n📊 预览统计:");
        for (TimeSegment segment : TimeSegment.values()) {
            if (segment.getProportion() > 0) {
                int segmentTasks = segmentPreview.getOrDefault(segment, 0);
                double percentage = (double) segmentTasks / totalPreviewTasks * 100;
                System.out.printf("   %s: %d 任务 (%.1f%%)%n",
                        segment.getDescription(), segmentTasks, percentage);
            }
        }
        System.out.printf("   预估每日总量: %d 任务%n", totalPreviewTasks);
    }

}
